#!/usr/bin/env node
const log = console.log;

let buf1=Buffer.alloc(256);
buf1[0]=0;

log('buf1 length:',buf1.length);
log('\nbuf1:',buf1);

//通过循环来初始化buffer中的每个字节
for(let i=0;i<buf1.length;i++){
  buf1[i]=i;
  };
  log('\nbuf1:',buf1);

//类似数组，对buffer进行切片操作
  let buf2=buf1.slice(250,256);
  log('\nbuf2:',buf2);

//在buffer中填充数据，buffer数据转化成JSON数据
  buf2.fill(0);
  log('\nbuf2:',buf2);
  log('\nbuf2\'s JSON:',buf2.toJSON());
  log('\nbuf2\'s JSON:',JSON.stringify(buf2));
 //用数组初始化buffer
  let array = ['a',0xba,0xdf,0x00,255,10];
  let buf3=Buffer.from(array);

  log('\nbuf3:',buf3.length,buf3);

  //用字符串初始化buffer
  let buf4=Buffer.from('hello world','utf8');
  log('\nbuf4:',buf4.length,buf4);

  //buffer数据的复制
  buf4.copy(buf3,0,0,buf3.length);
  log('\nbuf3:',buf3.length,buf3);

  //utf8编码
  let str='你好 lifangfang';
  let buf5=Buffer.from(str,'utf8');
  log('\n buffer length:',buf5.length);
  log('buffer content:',buf5);
  log('string length:',str.length);
}
